package com.gxitsky.utils;

import com.clearofchina.core.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    private static final String FOUR_STAR = "****";

    /**
     * 通过身份证号码获取出生日期、性别、年龄
     *
     * @param certificateNo
     * @return 返回的出生日期格式：1990-01-01 性别格式：F-女，M-男
     */
    public static Map<String, String> getBirAgeSex(String certificateNo) {
        String birthday = "";
        String age = "";
        String sexCode = "";
        int year = Calendar.getInstance().get(Calendar.YEAR);
        char[] number = certificateNo.toCharArray();
        boolean flag = true;

        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag)
                    return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag)
                    return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        }
        if (flag && certificateNo.length() == 15) {
            birthday = "19" + certificateNo.substring(6, 8);
            sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length()))
                    % 2 == 0 ? "F" : "M";
            age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
        } else if (flag && certificateNo.length() == 18) {
            birthday = certificateNo.substring(6, 10) + "/" + certificateNo.substring(10, 12) + "/"
                    + certificateNo.substring(12, 14);

            sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1))
                    % 2 == 0 ? "F" : "M";
            age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("birthday", birthday);
        map.put("age", age);
        map.put("sexCode", sexCode);
        return map;
    }

    /**
     * 根据身份证获取籍贯
     *
     * @param certNo
     * @return
     */
    public static String getNativePlace(String certNo) {
        if (StringUtils.isBlank(certNo) || certNo.length() < 2) {
            return "";
        }

        String[] a = {"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41",
                "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81",
                "82"};

        String[] b = {"北京市", "天津市", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省", "上海市", " 江苏省", "浙江省", "安徽省", "福建省",
                " 江西省", "山东省", " 河南省", "湖北省", " 湖南省", "广东省", " 广西壮族自治区", "海南省", "重庆市", "四川省", "贵州省", "云南省", " 西藏自治区",
                "陕西省", "甘肃省", "青海省", "宁夏回族自治区", "新疆维吾尔自治区", "台湾省", "香港特别行政区", "澳门特别行政区"};
        String pos = (certNo.substring(0, 2));// 获取身份证的前两位；
        int i;
        for (i = 0; i < a.length; i++) {
            if (pos.equals(a[i])) {
                break;
            }
        }
        return b[i]; // 获取b数组中的省份信息且输出省份;
    }

    public static String decCardNo(String cardNo) {
        if (StringUtils.isBlank(cardNo)) {
            return "";
        }

        int cardNoLen = cardNo.length();
        String desCardNo = "";

        switch (cardNoLen) {
            case 1:
            case 2:
                desCardNo = FOUR_STAR + cardNo;
                break;
            case 3:
            case 4:
            case 5:
            case 6:
                desCardNo = FOUR_STAR + cardNo.substring(cardNoLen - 2);
                break;
            case 7:
                desCardNo = FOUR_STAR + cardNo.substring(cardNoLen - 3);
                break;
            case 8:
                desCardNo = FOUR_STAR + cardNo.substring(cardNoLen - 4);
                break;
            case 9:
                desCardNo = cardNo.substring(0, 1) + FOUR_STAR + cardNo.substring(cardNoLen - 4);
                break;
            case 10:
                desCardNo = cardNo.substring(0, 2) + FOUR_STAR + cardNo.substring(cardNoLen - 4);
                break;
            case 11:
                desCardNo = cardNo.substring(0, 3) + FOUR_STAR + cardNo.substring(cardNoLen - 4);
                break;
            default:
                desCardNo = cardNo.substring(0, 4) + FOUR_STAR + cardNo.substring(cardNoLen - 4);
                break;
        }

        return desCardNo;
    }

    /**
     * 号码脱敏，前4后4
     *
     * @Param: [no]
     * @Return: java.lang.String
     * @Author: jingliang
     * @Date: 2021/4/26
     * @Version: V1
     **/
    public static String mask(String no) {
        int len = no.length();
        if (isContainChinese(no)) {
            return no.replaceAll("(.{1})(.*)(.{0})", "$1" + "**" + "$3");
        }
        if (len < 7) {
            return no.replaceAll("(.{1})(.*)(.{1})", "$1" + "**" + "$3");
        }
        return no.replaceAll("(.{" + (len < 12 ? 3 : 4) + "})(.*)(.{4})", "$1" + "****" + "$3");
    }

    public static String nameMask(String no) {
        int len = no.length();
        if (isContainChinese(no)) {
            if (len == 1) {
                return no;
            } else if (len <= 3) {
                return StringUtils.leftPad(StringUtils.right(no, 1), StringUtils.length(no), "*");
            } else if (len <= 6) {
                return StringUtils.leftPad(StringUtils.right(no, 2), StringUtils.length(no), "*");
            } else {
                return no.charAt(0) + StringUtils.leftPad(StringUtils.right(no, 2), StringUtils.length(no), "*").substring(1);
            }
        }
        String[] split = no.split("[^\\u4e00-\\u9fa5a-zA-Z]");
        if (split.length > 1) {
            String x = split[0].replaceAll("[\\w\\W]", "*");
            for (int i = 1; i < split.length; i++) {
                x += " " + split[i];
            }
            return x;
        }
        return no.replaceAll("(.{" + (len < 12 ? 3 : 4) + "})(.*)(.{4})", "$1" + "****" + "$3");
    }

    public static void main(String[] args) {
        String name1 = "张三";
        String name2 = "张三三";
        String name3 = "张三三三三";
        String name4 = "张三三三三三三";
        String name5 = "alsd jkla sjdsa";
        String name6 = "lan lkd";
        String name7 = "fdkj nvn dd aab";
        System.out.println(nameMask(name1));
        System.out.println(nameMask(name2));
        System.out.println(nameMask(name3));
        System.out.println(nameMask(name4));
        System.out.println(nameMask(name5));
        System.out.println(nameMask(name6));
        System.out.println(nameMask(name7));
    }

    /**
     * 判断字符串中是否含中文
     *
     * @Param: [str]
     * @Return: boolean
     * @Author: jingliang
     * @Date: 2021/4/26
     * @Version: V1
     **/
    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    /**
     * 姓名加密
     *
     * @param name
     * @return
     */
    public static String maskName(String name) {
        if (StringUtil.isBlank(name)) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < name.length() - 1; i++) {
            if (i < 3) {
                stringBuffer.append("*");
            }
        }
        stringBuffer.append(name.substring(name.length() - 1));
        return stringBuffer.toString();
    }

    /**
     * 手机号加密
     *
     * @param mobile
     * @return
     */
    public static String maskMobile(String mobile) {
        if (StringUtil.isBlank(mobile) || mobile.length() != 11) {
            return "";
        }
        return mobile.substring(0, 3) + "******" + mobile.substring(mobile.length() - 2);
    }


    /**
     * 电子健康卡加密 就是身份证加密
     *
     * @param electronicHealthCardNo 证件号
     * @return
     */
    public static String maskElectronicHealthCard(String electronicHealthCardNo) {
        if (StringUtil.isEmpty(electronicHealthCardNo)) {
            return "";
        }
        int length = electronicHealthCardNo.length();
        if (length <= 4) {
            String substring = electronicHealthCardNo.substring(1, electronicHealthCardNo.length() - 1);
            return electronicHealthCardNo.substring(0, 1) + substring.replaceAll("\\d", "*") + electronicHealthCardNo.substring(electronicHealthCardNo.length() - 1);
        } else if (length <= 8) {
            String substring = electronicHealthCardNo.substring(2, electronicHealthCardNo.length() - 2);
            return electronicHealthCardNo.substring(0, 2) + substring.replaceAll("\\d", "*") + electronicHealthCardNo.substring(electronicHealthCardNo.length() - 2);
        } else {
            String substring = electronicHealthCardNo.substring(4, electronicHealthCardNo.length() - 4);
            return electronicHealthCardNo.substring(0, 4) + substring.replaceAll("\\d", "*") + electronicHealthCardNo.substring(electronicHealthCardNo.length() - 4);
        }
    }

}


